home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / eb_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  5.2 KB  |  242 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  eb_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_STRATIFIED_H
  16. #define LEDA_STRATIFIED_H
  17.  
  18. //------------------------------------------------------------------------------
  19. //  stratified tree  (van emde boas tree)
  20. // 
  21. //  Stefan Naeher (1989)
  22. //
  23. //  Modification History:
  24. //
  25. //  - Use of bounded dictionaries instead of arrays
  26. //    ( bounded dictionaries are implemented by
  27. //      dynamic perfect hashing )
  28. //
  29. //      Michael Wenzel (1990)
  30. //
  31. //
  32. //  - Dynamization
  33. //
  34. //      Michael Wenzel (1990)
  35. //
  36. //
  37. //  - Function pred
  38. //
  39. //      Michael Wenzel (1990)
  40. //
  41. //
  42. //  - Minimum and maximum not recursively stored
  43. //
  44. //      Michael Wenzel (1991)
  45. //
  46. //
  47. //  - Class l_stratified for bot-structures
  48. //      with <= 2 Elements
  49. //
  50. //      Michael Wenzel (1991)
  51. //
  52. //
  53. //  - Union b_dict for dictionary
  54. //    ( Union of bounded dictionary and array )
  55. //
  56. //      Michael Wenzel (1991)
  57. //
  58. //
  59. //------------------------------------------------------------------------------
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Include & Makros
  63. //-----------------------------------------------------------------------------
  64.  
  65. #include <LEDA/impl/dp_hash.h>   
  66.  
  67.  
  68. #define pot_2(x)          ( 1 << x )
  69. #define mal_pot_2(x,y)    ( (x) << (y) )
  70. #define down(k)           ( (k) >> 1 )
  71. #define up(k)             ( down(k) + ((k) & 1) )
  72. #define high_bits(x)      ( x >> down(k) ) 
  73. #define low_bits(x)       ( x & (pot_2(down(k))-1) ) 
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. // definitions
  78. //-----------------------------------------------------------------------------
  79.  
  80. class l_stratified;
  81. class stratified;
  82.  
  83. typedef l_stratified* l_stratified_ptr;
  84. typedef stratified* stratified_ptr;
  85.  
  86. //-----------------------------------------------------------------------------
  87. // class definition 
  88. //-----------------------------------------------------------------------------
  89.  
  90.                                // union for dictionary
  91. class b_dict {
  92.  
  93.   union {
  94.  
  95.            GenPtr*         l;       // array
  96.            dp_hash*      d;       // bounded dictionary;
  97.  
  98.         };
  99.  
  100.  
  101. void insert(int x, GenPtr y, int k)
  102.                {
  103.                  if ( k <= 8 )  l[x] = y;        
  104.                  else           d->insert(GenPtr(x),y);  
  105.                            }
  106.  
  107. void del(int x, int k)     {
  108.                              if ( k <= 8 )  l[x] = 0; 
  109.                  else           d->del(GenPtr(x));
  110.                            }
  111.  
  112. GenPtr& lookup(int x, int k)  {
  113.                  if ( k <= 8 )  
  114.                    return l[x];
  115.                  else     
  116.                  { stp p = d->lookup(GenPtr(x));
  117.                    return d->info(p);
  118.                              }
  119.                            }
  120.  
  121.  b_dict(int);
  122. ~b_dict() {}
  123.  
  124. void clear(int);
  125.  
  126. LEDA_MEMORY(b_dict)
  127.  
  128. friend class stratified;
  129.  
  130. };
  131.  
  132. typedef b_dict* b_dict_ptr;
  133.  
  134.  
  135.                                // class for <= 2 elements
  136. class l_stratified {
  137.  
  138.  
  139. int          mi;      // minimum
  140. int          ma;      // maximum
  141.  
  142.  
  143. int size()                {
  144.                 if ( ma == -1 ) return 0 ;
  145.                 else if ( ma == mi ) return 1 ;
  146.                     else if ( ma <  mi ) return 2 ;
  147.                     else return 3;   // illegal, pointer was a stratified_ptr
  148.                   }
  149.  
  150. int insert(int x)         { if ( size() == 0 )   { mi = ma = x; return 1; }
  151.                 else if ( x < ma )   { ma = x; return 1; }
  152.                 else if ( x > mi )   { mi = x; return 1; }
  153.                 else return 0;
  154.               } 
  155.  
  156. int del(int x)            { if (size() == 1 && x == mi) { mi=ma=-1; return 1; }
  157.                 else if ( x == mi )   { mi = ma; return 1; }
  158.                 else if ( x == ma )   { ma = mi; return 1; }
  159.                 else return 0;
  160.               } 
  161.  
  162. int member(int x)         {
  163.                 return ( x == mi ) || ( x == ma );
  164.                           }
  165.  
  166. int max()                 { if ( size() <= 2 )
  167.                   return mi;
  168.                 else
  169.                   return ma;
  170.               }
  171.  
  172.  
  173. int min()                 { if ( size() <= 2 )
  174.                   return ma; 
  175.                 else
  176.                   return mi;
  177.               }
  178.  
  179. int succ(int x)           { if ( x < ma ) return ma;
  180.                 else if ( x < mi ) return mi;
  181.                 else return -1;
  182.                           }
  183.  
  184. int pred(int x)           { if ( x > mi ) return mi;
  185.                 else if ( x > ma ) return ma;
  186.                 else return -1;
  187.                           }
  188.  
  189. l_stratified(int x=-1)    { mi = ma = x; }
  190.  
  191. l_stratified(stratified*, int);
  192.  
  193.  
  194. LEDA_MEMORY(l_stratified)
  195.  
  196. friend class stratified;
  197. friend class b_dict;
  198.  
  199. };
  200.  
  201.  
  202.                                // recursive structure    
  203.  
  204. class stratified : public l_stratified { 
  205.  
  206. int            k;       // k-structure
  207. int            sz;      // size 
  208. l_stratified*  top;     // up(k)-structure
  209. b_dict_ptr     bot;     // pointer to bounded Dictionary of (k/2)-structures
  210.  
  211.  
  212. public:
  213.  
  214.  stratified(l_stratified*, int);
  215.  stratified(int);
  216. ~stratified();
  217.  
  218. int  min()        { return mi; }
  219. int  max()        { return ma; }
  220. int  size()       { return sz; }
  221.  
  222. int  min2();
  223. int  max2();
  224.  
  225. int  member(int);
  226. int  succ(int);
  227. int  pred(int);
  228.  
  229. int insert(int);
  230. int del(int);
  231.  
  232. void print();
  233.  
  234. LEDA_MEMORY(stratified)
  235.  
  236. };
  237.  
  238. typedef stratified eb_tree;
  239.  
  240. #endif
  241.